package com.witdata.controller.message;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.MessageDto;
import com.witdata.common.exception.GlobalException;
import com.witdata.common.domain.dto.MessageListDto;
import com.witdata.framework.service.message.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/message")
public class MessageController {
    private final MessageService messageService;


    @GetMapping("/list")
    public Result<MessageListDto> getMessageList(@RequestParam("sessionId") Long sessionId) {
        MessageListDto messageListDto = messageService.getMessageList(sessionId);
        return Result.ok(Status.SUCCESS, messageListDto);
    }

    @PostMapping("/send")
    public Result<String> sendMessage(@RequestBody MessageDto message) {
        if (ObjectUtils.isEmpty(message)
                || ObjectUtils.isEmpty(message.getSessionId())
                || ObjectUtils.isEmpty(message.getSenderId())
                || ObjectUtils.isEmpty(message.getContent())
        ) throw new GlobalException(Status.PARAMERROR);
        messageService.sendMessage(message);
        return Result.ok(Status.SUCCESS);
    }
    @GetMapping("/read")
    public Result<Status> readMessage(@RequestParam("sessionId") Long sessionId, @RequestParam("senderId") Long senderId) {
        messageService.readMessage(sessionId, senderId);
        return Result.ok(Status.READSUCCESS);
    }
}
